//
//  HDRegisterViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/15.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDRegisterViewController.h"
#import "HDVerifyPhoneNumberView.h"
#import "HDFillInfoView.h"
#import "HDCheckInfoView.h"

@interface HDRegisterViewController ()
@property(nonatomic,strong)HDRegisterInfo *registerInfo;
@property(nonatomic,strong)UIView *processView;
@property(nonatomic,strong)UIScrollView *contentScrollView;
@property(nonatomic,strong)HDVerifyPhoneNumberView *verifyPhoneView;
@property(nonatomic,strong)HDFillInfoView *fillInfoView;
@property(nonatomic,strong)HDCheckInfoView *checkInfoView;
@property(nonatomic,strong)UIImageView *commitSuccessView;
@end

@implementation HDRegisterViewController

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.verifyPhoneView];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.verifyPhoneView=nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.registerInfo = [[HDRegisterInfo alloc] init];
    self.view.backgroundColor = kContentViewColor;
    [self.view addSubview:self.processView];
    [self.view addSubview:self.contentScrollView];
    self.verifyPhoneView.registerInfo = self.registerInfo;
    self.fillInfoView.registerInfo = self.registerInfo;
    self.checkInfoView.registerInfo = self.registerInfo;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backAction) name:HDRegisterBackNotification object:nil];
}

#pragma mark - actions
- (void)backAction
{
    [self.view endEditing:YES];
    if (!self.contentScrollView.contentOffset.x)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self.contentScrollView setContentOffset:CGPointMake(self.contentScrollView.contentOffset.x-APPFRAME.size.width, 0) animated:YES];
    }
}

- (void)nextStepAction:(HDRegisterInfo *)registerInfo
{
    [self.view endEditing:YES];
    if (self.contentScrollView.contentOffset.x==APPFRAME.size.width)
    {
        self.checkInfoView.registerInfo = self.registerInfo;
    }
    if (self.contentScrollView.contentOffset.x!=APPFRAME.size.width*2)
    {
        [self.contentScrollView setContentOffset:CGPointMake(self.contentScrollView.contentOffset.x+APPFRAME.size.width, 0) animated:YES];
    }
    else
    {
        NSData* imgData=UIImagePNGRepresentation(self.registerInfo.avatarImg);
        NSString *imageString = [imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        
        NSData* driveCardImgData=UIImagePNGRepresentation(self.registerInfo.driveCardImg);
        NSString *driveCardImageString = [driveCardImgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        
        NSData* cocahCardImgData=UIImagePNGRepresentation(self.registerInfo.coachCardImg);
        NSString *cocahCardImageString = [cocahCardImgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        
        NSData* carCardImgData=UIImagePNGRepresentation(self.registerInfo.carCardImg);
        NSString *carCardImageString = [carCardImgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        
        NSMutableDictionary* params=[@{
                                   @"mobile_no":self.registerInfo.phoneNumber,
                                   @"password":self.registerInfo.password,
                                   @"verify_code":self.registerInfo.verifyCode,
                                   @"real_name":self.registerInfo.name,
                                   @"card_type":[NSNumber numberWithInt:0],
                                   @"card_no":self.registerInfo.identityCardId,
                                   @"coach_licenses":self.registerInfo.driveCardId
                               } mutableCopy];
        
        if (imageString!=nil&&imageString.length)
        {
            [params setObject:imageString forKey:@"user_picture"];
        }
        if (driveCardImageString!=nil&&driveCardImageString.length)
        {
            [params setObject:driveCardImageString forKey:@"driver_picture"];
        }
        if (cocahCardImageString!=nil&&cocahCardImageString.length)
        {
            [params setObject:cocahCardImageString forKey:@"licenses_picture"];
            [params setObject:[NSNumber numberWithInt:1] forKey:@"user_type"];
        }
        if (carCardImageString!=nil&&carCardImageString.length)
        {
            [params setObject:carCardImageString forKey:@"car_picture"];
            [params setObject:[NSNumber numberWithInt:2] forKey:@"user_type"];
        }
        [netEngine RequestRegisterWithTarget:self.VctrID hudView:self.contentScrollView Data:params];
    }
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"Register"]!=nil)
        {
            [self.processView removeFromSuperview];
            [self.contentScrollView removeFromSuperview];
            [self.view addSubview:self.commitSuccessView];
        }
    }
}

- (void)backToRootVC:(UIButton *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (scrollView==self.contentScrollView) {
        for (int i=0; i<3; i++) {
            UIButton *processBtn = (UIButton *)[self.processView viewWithTag:100+i];
            if (self.contentScrollView.contentOffset.x/APPFRAME.size.width==i) {
                processBtn.selected = YES;
            } else {
                processBtn.selected = NO;
            }
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

#pragma mark - getters
- (UIView *)processView
{
    if (!_processView) {
        _processView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, APPFRAME.size.width, 44)];
        _processView.backgroundColor = [UIColor whiteColor];
        NSArray *btnTitleArray = @[@"验证手机",@"填写资料",@"审核身份"];
        for (int i=0; i<3; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitleColor:[UIColor hexStringToColor:@"#e94929"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [btn setTitle:btnTitleArray[i] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"验证_未选中"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"验证_选中"] forState:UIControlStateSelected];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.userInteractionEnabled = NO;
            [_processView addSubview:btn];
            btn.tag = 100+i;
        }
        UIButton *firstBtn = (UIButton *)[_processView viewWithTag:100];
        firstBtn.frame = CGRectMake(20, 12, 60, 20);
        firstBtn.selected = YES;
        UIButton *secondBtn = (UIButton *)[_processView viewWithTag:101];
        secondBtn.frame = CGRectMake(APPFRAME.size.width/2.0-30, 12, 60, 20);
        UIButton *thirdBtn = (UIButton *)[_processView viewWithTag:102];
        thirdBtn.frame = CGRectMake(APPFRAME.size.width-80, 12, 60, 20);
        for (int i=0; i<2; i++) {
            UIImageView *arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*APPFRAME.size.width/3.0-5, 20, 10, 5)];
            arrowImage.image = [UIImage imageNamed:@"箭头"];
            [_processView addSubview:arrowImage];
        }
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, APPFRAME.size.width, 0.5)];
        line.image = [UIImage imageNamed:@"dot"];
        [_processView addSubview:line];
    }
    return _processView;
}

- (UIScrollView *)contentScrollView
{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.processView.bottom, APPFRAME.size.width, APPFRAME.size.height-44-self.processView.height)];
        _contentScrollView.scrollEnabled = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.delegate = (id)self;
        _contentScrollView.contentSize = CGSizeMake(APPFRAME.size.width*3, _contentScrollView.height);
        [_contentScrollView addSubview:self.verifyPhoneView];
        [_contentScrollView addSubview:self.fillInfoView];
        [_contentScrollView addSubview:self.checkInfoView];
    }
    return _contentScrollView;
}

- (HDVerifyPhoneNumberView *)verifyPhoneView
{
    if (!_verifyPhoneView) {
        _verifyPhoneView = [[HDVerifyPhoneNumberView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, APPFRAME.size.height-44-self.processView.height)];
        _verifyPhoneView.delegate = (id)self;
    }
    return _verifyPhoneView;
}

- (HDFillInfoView *)fillInfoView
{
    if (!_fillInfoView) {
        _fillInfoView = [[HDFillInfoView alloc] initWithFrame:CGRectMake(APPFRAME.size.width, 0, APPFRAME.size.width, APPFRAME.size.height-44-self.processView.height)];
        _fillInfoView.delegate = (id)self;
    }
    return _fillInfoView;
}

- (HDCheckInfoView *)checkInfoView
{
    if (!_checkInfoView) {
        _checkInfoView = [[HDCheckInfoView alloc] initWithFrame:CGRectMake(APPFRAME.size.width*2, 0, APPFRAME.size.width, APPFRAME.size.height-44-self.processView.height)];
        _checkInfoView.delegate = (id)self;
    }
    return _checkInfoView;
}

- (UIImageView *)commitSuccessView
{
    if (!_commitSuccessView) {
        CGFloat width = APPFRAME.size.width-20;
        _commitSuccessView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (APPFRAME.size.width-44-width*700/600.0)/2.0, width, width*700/600.0)];
        _commitSuccessView.userInteractionEnabled = YES;
        _commitSuccessView.image = [UIImage imageNamed:@"资料提交成功"];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, _commitSuccessView.width, 30)];
        titleLabel.font = [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = @"您的资料已成功提交！";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [_commitSuccessView addSubview:titleLabel];
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, APPFRAME.size.height/4.0, _commitSuccessView.width, 40)];
        tipLabel.textColor = kLightGrayColor;
        tipLabel.font = [UIFont systemFontOfSize:13];
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.numberOfLines = 0;
        tipLabel.text = @"客服会在3个工作日内审核完毕，\n如有疑问可致电4008-0551-0558";
        [_commitSuccessView addSubview:tipLabel];
        UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        returnBtn.frame = CGRectMake(_commitSuccessView.width/2.0-40, tipLabel.bottom+20, 80, 30);
        [returnBtn setTitle:@"返回首页" forState:UIControlStateNormal];
        [returnBtn setTitleColor:[UIColor hexStringToColor:@"#e94929"] forState:UIControlStateNormal];
        [returnBtn setBackgroundImage:[UIImage imageNamed:@"验证_未选中"] forState:UIControlStateNormal];
        [returnBtn setBackgroundImage:[UIImage imageNamed:@"验证_选中"] forState:UIControlStateSelected];
        returnBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [returnBtn addTarget:self action:@selector(backToRootVC:) forControlEvents:UIControlEventTouchUpInside];
        [_commitSuccessView addSubview:returnBtn];
        UIImageView *bottomImage = [[UIImageView alloc] initWithFrame:CGRectMake(_commitSuccessView.width/2.0-45, _commitSuccessView.height-74, 90, 44)];
        bottomImage.image = [UIImage imageNamed:@"资料提交_速考通"];
        [_commitSuccessView addSubview:bottomImage];
    }
    return _commitSuccessView;
}

@end
